#!/usr/bin/python3
# -*- coding: utf-8 -*-

import re

import allure
from playwright.sync_api import Page

from models.base_page import BasePage
from utils.log import log


class RoutingTablePage(BasePage):
    def __init__(self, page: Page):
        super().__init__(page)
        self.route = "routing_tables"

    @allure.step("输入路由表的名称")
    def type_route_table_name(self, route_table_name):
        self.page.locator("input[name=\"routing_table_name\"]").fill(route_table_name)

    @allure.step("选择私有网络类型")
    def click_vxnet_type(self):
        self.page.get_by_label("私有网络").check()

    @allure.step("选择负载均衡器类型")
    def click_lb_type(self):
        self.page.get_by_label("负载均衡器").check()

    def check_create_route_table_popout_title_to_be_visible(self):
        """创建路由表弹框标题"""
        self.expect.to_be_visible(self.page.get_by_role("heading", name="创建路由表 "))

    def get_delete_route_table_alert_info_text_content(self):
        """弹框组件-获取弹框内容文本"""
        if self.page.locator("#modalId").locator(".alert-info").is_visible():
            alert_text_content = (
                self.page.locator("#modalId").locator(".alert-info").text_content()
            )
            log.info(f"弹框文本:{alert_text_content}")
            return alert_text_content

    def check_rtable_page_user_guide_element_to_be_visible(self):
        """检查使用指南元素是否可见"""
        with allure.step("检查RoutingTablePage:使用指南元素是否可见"):
            self.expect.to_be_visible(self.page.locator(".intro"))  # 使用指南栏目
            self.expect.to_be_visible(self.page.get_by_text("使用指南"))
            self.expect.to_be_visible(self.page.get_by_text("收起"))
            self.expect.to_be_visible(self.page.locator(".svg-icon > use"))
            self.expect.to_contain_text(self.page.locator("#console"),
                                        "路由表 ( Routing Table ) 可以为资源提供 IP 网络的路由选择，"
                                        "通过配置目标网络及其下一跳，指定网络的路由方向，从而实现网络的连通和优化。")

    def check_rtable_page_table_element_to_be_visible(self):
        """检查工具栏及table是否可见"""
        with allure.step("检查RoutingTablePage:工具栏及table是否可见"):
            self.expect.to_be_visible(self.page.locator(".table-toolbar"))
            self.expect.to_be_visible(self.page.get_by_role("link", name="G", exact=True))
            self.expect.to_be_visible(self.page.locator("a").filter(has_text=re.compile(r"^创建$")))
            self.expect.to_be_visible(self.page.locator("select[name=\"search_key\"]"))
            self.expect.to_be_visible(self.page.get_by_placeholder(" "))
            self.expect.to_be_visible(self.page.locator("select[name=\"limit\"]"))
